package fr.xgouchet.wedding.client.view;

import java.util.HashMap;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratedStackPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

import fr.xgouchet.wedding.client.presenter.NavigationPresenter;

/**
 * UI Widget for the Navigation bar
 * 
 * @author x.gouchet
 * 
 */
public class NavigationView extends Composite implements
		NavigationPresenter.Display {

	/**
	 * Default Constructor
	 */
	public NavigationView() {
		// Main panel
		VerticalPanel main = new VerticalPanel();
		initWidget(main);

		mStackPanel = new DecoratedStackPanel();
		mLabel = new Label();
		mLabel.addStyleName("navMsg");

		mPanels = new HashMap<String, Panel>();

		main.add(mStackPanel);
		main.add(mLabel);
	}

	/**
	 * @see NavigationPresenter.Display#addData(String,String,String)
	 */
	public void addData(String data, String cat, String token) {
		if (!mPanels.containsKey(cat)) {
			VerticalPanel panel = new VerticalPanel();
			mPanels.put(cat, panel);
			mStackPanel.add(panel, cat);
		}
		mPanels.get(cat).add(new Hyperlink(data, token));

	}

	/**
	 * @see NavigationPresenter.Display#clearData()
	 */
	public void clearData() {
		mStackPanel.clear();
	}

	/**
	 * @see NavigationPresenter.Display#setVisibleCategory(String)
	 */
	public void setVisibleCategory(String cat) {
		int index = -1;
		if (mPanels.containsKey(cat)) {
			index = mStackPanel.getWidgetIndex(mPanels.get(cat));
			mStackPanel.showStack(index);
		}
	}

	/**
	 * @see NavigationPresenter.Display#setMessage(String)
	 */
	public void setMessage(String msg) {
		mLabel.setText(msg);
	}

	/**
	 * @see NavigationPresenter.Display#asWidget()
	 */
	public Widget asWidget() {
		return this;
	}

	/** The stack panel */
	private DecoratedStackPanel mStackPanel;

	/** the map matching categories with the corresponding panel */
	private HashMap<String, Panel> mPanels;

	/** */
	private Label mLabel;
}
